﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System;
using Caliburn.Micro;
using OI.DataModel;
using OI.Framework;
using MenuItem = OI.Framework.MenuItem;

namespace OI.ViewModels
{
    public class DatasetDetailsViewModel : Conductor<IScreen>.Collection.OneActive
    {
        #region
        public string Title { get { return DatasetDetail.Name.TruncateOnWordBoundary(Configuration.DataDetailNameMaxStringLength); } }
        public bool IsMapAvailable { get { return DatasetDetail.IsSpatial && !string.IsNullOrEmpty(DatasetDetail.LayerID); } }
        public bool IsDataAvailable { get { return !string.IsNullOrEmpty(DatasetDetail.TableName); } }

        // bound to left details section
        public BindableCollection<Tuple<string, string>> Items { get; set; }
       
        // the dataset we are detailing
        public OIDataset DatasetDetail
        {
            get
            {
                return _DatasetDetail;
            }
            set
            {
                _DatasetDetail = value;

                if (_DatasetDetail != null)
                {
                    // setup items - this is the left header/value listing datasource
                    Items = new BindableCollection<Tuple<string, string>> 
                    {
                        new Tuple<string, string>("description", _DatasetDetail.Description),
                        new Tuple<string, string>("category", _DatasetDetail.CategoryName),
                        new Tuple<string, string>("source", _DatasetDetail.SourceName),
                        new Tuple<string, string>("published date", _DatasetDetail.FormattedPublishedDate),
                        new Tuple<string, string>("tags", _DatasetDetail.Tags),
                        new Tuple<string, string>("update frequency", _DatasetDetail.UpdateFrequency),
                        new Tuple<string, string>("contact email", _DatasetDetail.ContactEmail),
                    };
                    
                    NotifyOfPropertyChange(() => Title);
                    NotifyOfPropertyChange(() => Items);
                }
            }
        }
        private OIDataset _DatasetDetail;


        // the menu items for selecting the type of view
        public BindableCollection<MenuItem> MainMenu
        {
            get
            {
                return new BindableCollection<MenuItem>
                {
                    new MenuItem
                    {
                        Header = "map", Width = 100, IsEnabled = IsMapAvailable, IsSelected = true
                    },
                    new MenuItem()
                    {
                        Header = "data", Width = 100, IsEnabled = IsDataAvailable, IsSelected = false
                    }
                };
            }
        }

        #endregion


        // constructor requires dataset to be passed in
        public DatasetDetailsViewModel(OIDataset dataset)
        {
            DatasetDetail = dataset;
        }


        // on view loaded, dynamically setup datagrid
        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);

            // load samples
            if (IsMapAvailable)
            {
                SelectView(DatasetDetailsOptionsPanelViewModel.Modes.Map);
            }
            else if (IsDataAvailable)
            {
                SelectView(DatasetDetailsOptionsPanelViewModel.Modes.Data);
            }
        }


        // map/data selection menu action
        public void MenuSelect(MenuItem item)
        {
            if (item.Header == "data")
            {
                SelectView(DatasetDetailsOptionsPanelViewModel.Modes.Data);
            }
            else if (item.Header == "map")
            {
                SelectView(DatasetDetailsOptionsPanelViewModel.Modes.Map);
            }
        }


        // map or data view
        private void SelectView(DatasetDetailsOptionsPanelViewModel.Modes mode)
        {
            if (mode == DatasetDetailsOptionsPanelViewModel.Modes.Data)
            {
                var vm = IoC.Get<DatasetDetailsDataViewModel>();
                vm.DatasetDetail = DatasetDetail;
                ActivateItem(vm);
            }
            else if (mode == DatasetDetailsOptionsPanelViewModel.Modes.Map)
            {
                var vm = IoC.Get<DatasetDetailsMapViewModel>();
                vm.DatasetDetail = DatasetDetail;
                ActivateItem(vm);
            }
            IoC.Get<DatasetDetailsOptionsPanelViewModel>().Mode = mode;
        }
    }
}
